/*
 * JOY电子商务网运营事业部
 * 中国移动通信集团设计院有限公司
 * B2BJOY.COM  OPERATION  DIVISION
 * China Mobile Group Design Institute Co., Ltd
 */

package com.uhaou.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.hibernate.validator.constraints.Length;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.web.scope.Flash;
import cn.org.rapid_framework.web.util.HttpUtils;

import com.uhaou.common.base.BaseStruts2Action;
import com.uhaou.common.util.EasyUIJsonUtil;
import com.uhaou.common.util.Json; 
import com.uhaou.system.model.*; 
import com.uhaou.system.service.*; 
import com.uhaou.system.vo.query.*; 
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * @author  jiangcheng
 * @version 1.0
 * @since 1.0
 */


public class SysRoleAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String QUERY_JSP = "/admin/sysRole/query.jsp";
	protected static final String LIST_JSP= "/admin/sysRole/list.jsp";
	protected static final String CREATE_JSP = "/admin/sysRole/create.jsp";
	protected static final String EDIT_JSP = "/admin/sysRole/edit.jsp";
	protected static final String SHOW_JSP = "/admin/sysRole/show.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/admin/sysRole/list.do";
	
	private ISysRoleManager sysRoleManager;
	private ISysFuncRoleManager sysFuncRoleManager;
	private ISysFunctionManager sysFunctionManager;
	
	
	
	private SysRole sysRole;
	java.lang.String id = null;
	private String[] items;
	private String param;
	
	public void setParam(String param){
		this.param = param;
	}

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			sysRole = new SysRole();
		} else {
			sysRole = (SysRole)sysRoleManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setSysRoleManager(ISysRoleManager manager) {
		this.sysRoleManager = manager;
	}	
	public void setSysFunctionManager(ISysFunctionManager sysFunctionManager) {
		this.sysFunctionManager = sysFunctionManager;
	}
	public void setSysFuncRoleManager(ISysFuncRoleManager manager) {
		this.sysFuncRoleManager = manager;
	}	
	
	public Object getModel() {
		return sysRole;
	}
	
	public void setId(java.lang.String val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	/** 执行搜索 */
	public String list() {
		return LIST_JSP;
	}
	
	public String listAjax(){
		SysRoleQuery query = newQuery(SysRoleQuery.class,DEFAULT_SORT_COLUMNS);
		query.setDeleted("0");
		Page page = sysRoleManager.findPage(query);
//		savePage(page,query);
		Map map = new HashMap();
		map.put("total", page.getTotalCount());
		map.put("rows", page.getResult());
		return writeAjaxResponse(Json.toJson(map));
	}

	/**
	 * 解决同一URL的权限控制zhanghao
	 * @return
	 */
	public String listAjaxuser(){
		return this.listAjax();
	}
	/**
	 * 解决同一URL的权限控制zhanghao
	 * @return
	 */
	public String listAjaxdept(){
		return this.listAjax();
	}

	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		if (param != null) {
			//根据传递过来的参数判断是否是修改角色操作。。。如果是修改操作则根据角色ID获得所有的角色权限关联表的集合
			SysFuncRoleQuery frquery = newQuery(SysFuncRoleQuery.class,DEFAULT_SORT_COLUMNS);
			Page frpage = null;
			List<SysFuncRole> frList = null;
			
			SysFunctionQuery query = newQuery(SysFunctionQuery.class,DEFAULT_SORT_COLUMNS);
			query.setId("");
			query.setPageSize(10000);
			query.setDeleted("0");
//			query.setFuncType("1");
			Page page = sysFunctionManager.findPage(query);
			List<SysFunction> funcList = page.getResult();
			List<Map> treedataList = new ArrayList<Map>();
			for(SysFunction t : funcList){
				Map<String, Object> map = new HashMap();
				map.put("id", t.getId());
				map.put("parentId", t.getParentId());
				map.put("order", 1);
				map.put("text", t.getFuncName());
				map.put("iconCls", "icon-func");
				treedataList.add(map);
			}
			Map<String, Object> root = new HashMap();
			root.put("id", "-1");
			root.put("text", "权限列表");
			root.put("iconCls", "icon-ok");
			//增加顶级节点的deptcode。。以便页面上生成顶级节点的子部门时出错
			Map amap = new HashMap();
			amap.put("deptCode", "");
			root.put("attributes", amap);
			//root.put("checked", "true");
			String jsonStr = EasyUIJsonUtil.createTreeJsonString(root,
					treedataList);
			return writeAjaxResponse(jsonStr);
		}
		return CREATE_JSP;
	}
	
	/** 保存新增对象 */
	public String save() {
		sysRoleManager.save(sysRole);
		Flash.current().success(CREATED_SUCCESS); //存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return LIST_ACTION;
	}
	
	/**进入更新页面*/
	public String edit() {
		if(this.id!=null&&!this.id.equals("")){
			SysRole t = this.sysRoleManager.getById(this.id);
			Map<String, Object> map = new HashMap();
			map.put("id", t.getId());
			map.put("roleName", t.getRoleName());
			map.put("createTime", t.getCreateTimeString()!=null?t.getCreateTimeString().trim():"");
			map.put("deleted", t.getDeleted());
			map.put("creator", t.getCreator());
			map.put("memo", t.getMemo());
			return writeAjaxResponse(Json.toJson(map));
		}else{
			return writeAjaxResponse("");
		}
	}
	
	/**保存更新对象*/
	public String update() {
		sysRoleManager.update(this.sysRole);
		Flash.current().success(UPDATE_SUCCESS);
		return LIST_ACTION;
	}
	
	/**删除对象*/
	public String delete() {
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			java.lang.String id = new java.lang.String((String)params.get("id"));
			sysRoleManager.removeById(id);
		}
		Flash.current().success(DELETE_SUCCESS);
		return LIST_ACTION;
	}
	
	/**插入角色同时关联角色和权限即插入角色权限关联表数据 zhanghao*/
	public String saveRoleandFunc() {
		String [] funcid = null;
		if (param != null&&!param.equals("")) {
			funcid = param.split(",");
		}
		sysRoleManager.save(sysRole, funcid);
		Flash.current().success(DELETE_SUCCESS);
		return LIST_ACTION;
	}
	
	/**更新角色同时关联角色和权限即插入角色权限关联表数据 zhanghao*/
	public String updateRoleandFunc() {
		if(this.sysRole==null||this.sysRole.getId()==null||this.sysRole.getId().equals("")||param==null){
			return LIST_ACTION;
		}
		String [] funcid = null;
		if (param != null&&!param.equals("")) {
			funcid = param.split(",");
		}
		sysRoleManager.update(this.sysRole, funcid);
		Flash.current().success(UPDATE_SUCCESS);
		return LIST_ACTION;
	}
	
	/** 进入新增页面*/
	public String editbyid() {
		if (param != null) {
			//根据传递过来的参数判断是否是修改角色操作。。。如果是修改操作则根据角色ID获得所有的角色权限关联表的集合
			SysFuncRoleQuery frquery = newQuery(SysFuncRoleQuery.class,DEFAULT_SORT_COLUMNS);
			Page frpage = null;
			List<SysFuncRole> frList = null;
			List funcidlist = new ArrayList();
			if(this.id!=null&&!this.id.equals("")){
				frquery.setId("");
				frquery.setPageSize(10000);
				frquery.setRoleId(this.id);
				frpage = sysFuncRoleManager.findPage(frquery);
				frList = frpage.getResult();
				for(int i=0;frList!=null&&i<frList.size();i++){
					funcidlist.add((frList.get(i)).getFuncId());
				}
			}
			
			SysFunctionQuery query = newQuery(SysFunctionQuery.class,DEFAULT_SORT_COLUMNS);
			query.setId("");
			query.setPageSize(10000);
			query.setDeleted("0");
//			query.setFuncType("1");
			Page page = sysFunctionManager.findPage(query);
			List<SysFunction> funcList = page.getResult();
			List<Map> treedataList = new ArrayList<Map>();
			for(SysFunction t : funcList){
				Map<String, Object> map = new HashMap();
				map.put("id", t.getId());
				map.put("parentId", t.getParentId());
				map.put("order", 1);
				map.put("text", t.getFuncName());
				map.put("iconCls", "icon-func");
				if(funcidlist!=null&&funcidlist.contains(t.getId())){
					map.put("checked", true);
				}
				treedataList.add(map);
			}
			Map<String, Object> root = new HashMap();
			root.put("id", "-1");
			root.put("text", "权限列表");
			root.put("iconCls", "icon-ok");
			//增加顶级节点的deptcode。。以便页面上生成顶级节点的子部门时出错
			Map amap = new HashMap();
			amap.put("deptCode", "");
			root.put("attributes", amap);
			//root.put("checked", "true");
			String jsonStr = EasyUIJsonUtil.createTreeJsonString(root,
					treedataList);
			return writeAjaxResponse(jsonStr);
		}
		return CREATE_JSP;
	}
	/**
	 * 根据id的集合批量删除角色对象 zhanghao
	 * @return
	 */
	public String deleteByIds(){
		if (param != null&&!param.equals("")) {
			List list = new ArrayList();
			for(int i=0;i<param.split(",").length;i++){
				list.add(param.split(",")[i]);
			}
			this.sysRoleManager.deleteByIds(list);
			return writeAjaxResponse("1");
		}
		return writeAjaxResponse("");
	}
}
